package com.init.bootframe.util;

import java.util.Properties;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import org.apache.tomcat.util.codec.binary.Base64;
import sun.misc.BASE64Decoder;

public class TmompEncryptUtil {

	/**
	 * 流转base64字符串
	 * 
	 * @param bytes
	 * @return
	 */
	public static String base64Encode(byte[] bytes) {
		return Base64.encodeBase64String(bytes);
	}

	/**
	 * base64字符串转流
	 * 
	 * @param base64Code
	 * @return
	 * @throws Exception
	 */
	public static byte[] base64Decode(String base64Code) throws Exception {
		return new BASE64Decoder().decodeBuffer(base64Code);
	}

	/**
	 * 字符流加密
	 * 
	 * @param content
	 * @return 加密后返回字符串
	 * @throws Exception
	 */
	public static byte[] aesEncryptToBytes(String content) throws Exception {
		// 获取AES加密配置信息
		Properties settings = TmompUtils.loadProperties("settings.properties");
		String aes_algorithmstr = settings.getProperty(TmompConstant.SettingsConstant.PARAMER_AES_ALGORITHMSTR);
		String aes_key = settings.getProperty(TmompConstant.SettingsConstant.PARAMER_AES_KEY);
		// 加密处理
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		kgen.init(128);
		Cipher cipher = Cipher.getInstance(aes_algorithmstr);
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(aes_key.getBytes(), "AES"));
		return cipher.doFinal(content.getBytes(TmompConstant.BaseDataConstant.INPUT_STREAM_READER_ENCODING));
	}

	/**
	 * 字符串加密
	 * 
	 * @param content
	 * @return 加密后返回字符串
	 * @throws Exception
	 */
	public static String aesEncrypt(String content) throws Exception {
		return base64Encode(aesEncryptToBytes(content));
	}

	/**
	 * 字符流解密
	 * 
	 * @param encryptBytes
	 * @return 加密后返回字符串
	 * @throws Exception
	 */
	public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {
		// 获取AES加密配置信息
		Properties settings = TmompUtils.loadProperties("settings.properties");
		String aes_algorithmstr = settings.getProperty(TmompConstant.SettingsConstant.PARAMER_AES_ALGORITHMSTR);
		String aes_key = settings.getProperty(TmompConstant.SettingsConstant.PARAMER_AES_KEY);
		// 解密处理
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		kgen.init(128);
		Cipher cipher = Cipher.getInstance(aes_algorithmstr);
		cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aes_key.getBytes(), "AES"));
		byte[] decryptBytes = cipher.doFinal(encryptBytes);
		return new String(decryptBytes, TmompConstant.BaseDataConstant.INPUT_STREAM_READER_ENCODING);
	}

	/**
	 * 字符串解密
	 * 
	 * @param encryptStr
	 * @return 加密后返回字符串
	 * @throws Exception
	 */
	public static String aesDecrypt(String encryptStr) throws Exception {
		return aesDecryptByBytes(base64Decode(encryptStr));
	}

	/**
	 * 测试
	 * 
	 */
	public static void main(String[] args) throws Exception {

		String content = "Test String么么哒";
		System.out.println("加密前：" + content);

		String encrypt = aesEncrypt(content);
		System.out.println(encrypt.length() + ":加密后：" + encrypt);

		String decrypt = aesDecrypt(
				"suujBTGNekjAgpOPd2aXyCPnxAtido9rmHeu+e0WNYr1s1hMUMA+5l8DPM6VpPq1LuoboTFj0Sm0LhI3RYS810X6nlIIVNJloVYmrLsLXARKP30jAgJ71YpTfS+FH8kj");
		System.out.println("解密后：" + decrypt);
	}
}